package com.hc.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hc.dao.IRepairSettingDAO;
import com.hc.entity.RRepairSetting;
import com.hc.exception.FieldNotExistException;
import com.hc.service.IDataDictService;
import com.hc.service.IRepairSettingService;
import com.hc.vo.AddRepairSettingVO;
import com.hc.vo.GetRepairSettingVO;
import com.hc.vo.PagingVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Service;

import javax.validation.constraints.Null;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

@Service
public class RepairSettingServiceImpl implements IRepairSettingService {

    IRepairSettingDAO repairSettingDAO;
    IDataDictService dataDictService;

    @Autowired
    public RepairSettingServiceImpl(IRepairSettingDAO repairSettingDAO, IDataDictService dataDictService) {
        this.repairSettingDAO = repairSettingDAO;
        this.dataDictService = dataDictService;
    }

    public List<RRepairSetting> getAll() {
    return     repairSettingDAO.getInfoByConditions(new RRepairSetting());
    }

    @Override
    public PageInfo<RRepairSetting> getPaged(int pageNum, int pageSize, @Nullable String typeName, @Nullable String repairWay) {
        Page<RRepairSetting> page = PageHelper.startPage(pageNum, pageSize);
        RRepairSetting conditions = new RRepairSetting();
        conditions.setRepairWay(repairWay);
        conditions.setRepairTypeName(typeName);
        PageInfo<RRepairSetting> pageInfo = page.doSelectPageInfo(() -> {
            repairSettingDAO.getInfoByConditions(conditions);
        });
        //查询字典并赋值
        pageInfo.getList().forEach(ent -> {
            dataDictService.fillValByDictIfExist(ent);
        });
        page.close();
        return pageInfo;
    }

    @Override
    public void updateSetting(RRepairSetting setting) {
        if (repairSettingDAO.updateById(setting) <= 0) {
            throw new FieldNotExistException("该设置已经删除");
        }
    }

    @Override
    public RRepairSetting getOne(String id) {
        RRepairSetting data = repairSettingDAO.getBySettingId(id);
        if (data == null) {
            throw new FieldNotExistException("该设置已经删除");
        }
        return data;
    }

    public void addSetting(RRepairSetting rRepairSetting) {
        rRepairSetting.setCommunityId("2022081539020475");
        rRepairSetting.setSettingId(String.valueOf(System.currentTimeMillis()));
        repairSettingDAO.addRepairSetting(rRepairSetting);
    }

    @Override
    public void deleteSetting(String settingId) {
        if (repairSettingDAO.deleteByBId(settingId) <= 0) {
            throw new FieldNotExistException("该设置已经删除");
        }
    }
}
